package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.dataBase.UserDatabaseHelper;

public class registerActivity extends AppCompatActivity {
    private UserDatabaseHelper User_database;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        EditText user;
        EditText password;
        EditText repassword;
        Button register_button;
        TextView login_link;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);

        user = (EditText)findViewById(R.id.regiser_username) ;
        password = (EditText)findViewById(R.id.regiser_password);
        repassword = (EditText)findViewById(R.id.regiser_confire_password);
        register_button = (Button)findViewById(R.id.register_button);
        login_link = (TextView)findViewById(R.id.login_link);

        User_database = new UserDatabaseHelper(registerActivity.this,"User.dp",null,2);


        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String User = user.getText().toString();
                String Password = password.getText().toString();
                String Repassword = repassword.getText().toString();
                if(Utils.isValidPassword(Password) && Utils.isValidUsername(User)){
                    if(User_database.checkUsername(User)){
                        Toast.makeText(registerActivity.this,"用户名已存在",Toast.LENGTH_SHORT).show();
                    }
                    else{
                        if(Utils.isEqualPassword(Password,Repassword)){
                            //创建用户
                            User_database.insertData(User,Password);
                            Toast.makeText(registerActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(registerActivity.this, LoginActivity.class);
                            startActivity(intent);
                        }else {
                            Toast.makeText(registerActivity.this,"两次输入密码不同，请重试",Toast.LENGTH_SHORT).show();
                        }
                    }

                }
            }
        });

        login_link.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(registerActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
    }
}